import React, { Component } from "react";
// ant
import { Modal, message } from "antd";
// API
import http from '../../../utils/request';
// 组件
import FormCom from "@c/form/Index";
// Store
import Store from "@/stroe/Index";


class GroupDetailModal extends Component {
    constructor(props){
        super(props);
        this.state = {
            isModalVisible: false,
            group_id: "",
            formConfig: {
                url: "groupBind",
                editKey: "",
                initValue: {
                    number: 0,
                    status: "",
                    parentId: ""
                },
                setFieldValue: {},
                formatFormKey: "id"
            },
            formLayout: {
                labelCol: { span: 4 },
                wrapperCol: { span: 20 }
            },
            formItem: [
                { 
                    type: "SelectComponent",
                    label: "服务", 
                    name: "service_id", 
                    url: "serviceList",
                    method: "get",
                    required: true, 
                    propsKey: {
                        label: "code",
                        value: "id"
                    },
                    style: { width: "200px" },
                    placeholder: "请选择服务"
                },
                { 
                    type: "SelectComponent",
                    label: "机器", 
                    filter_option: true,
                    mode: "multiple",
                    name: "machine_id", 
                    url: "galaxy_machineList",
                    method: "get",
                    required: true, 
                    propsKey: {
                        label: "inner_ip",
                        value: "id"
                    },
                    placeholder: "请选择机器",
                }
            ]
        };
    }

    componentDidMount(){
        this.props.onRef(this);
    }

    onFormRef = (ref) => {
        this.child = ref;
    }

    // 关闭弹窗
    handleCancel = () => {
        // 清单表单
        this.child.onReset();
        this.setState({
            isModalVisible: false,  // 关闭弹窗
            formConfig: { setFieldValue: "" },  // 清除form的初始数据
        })
    }
    
    /** 弹窗 */
    visibleModal = (params) => {
        console.log(params)
        this.setState({
            isModalVisible: params.status,
            group_id: params.group_id
        })
    }

    onBlurEvent = (e) => {
        const value = e.currentTarget.value;
        if(value) {
            this.updateItem(value ? false : true);
            return false;
        }
    }
    // 提交按钮
    submit = (value) => {
        this.handlerFormAdd(value);
        
    }
    // 添加 提交表单
    handlerFormAdd= (value) => {
        const requestData = value;
        requestData.department_code = Store.getState().app['department'];
        requestData.group_id = this.state.group_id;
        console.log(requestData)
        http.post('/avatar', requestData).then(response => {
            const responseData = response.data;
            // 提示
            message.info(responseData.msg);
            // 关闭弹准备
            this.handleCancel(false);
        })
    }

    render(){
        return (
            <Modal title="添加群组机器" visible={this.state.isModalVisible} footer={null} onCancel={this.handleCancel}>
                <FormCom onRef={this.onFormRef} onBlur={this.onBlurEvent} formItem={this.state.formItem} formLayout={this.state.formLayout} formConfig={this.state.formConfig} submit={this.submit}>
                </FormCom>
            </Modal>
        )
    }
}

export default GroupDetailModal;